<?php
namespace app\system\controller;

use app\common\model\CmsCategoryModel;
use think\Request;

class CmsCategoryController extends BaseController
{

    private $cmsCategoryModel;
    private $cmsCategoryCache;
    private $cmsMoudleCache;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->cmsCategoryModel = new CmsCategoryModel();
        $this->cmsCategoryCache = cache('cms_category');
        $this->cmsMoudleCache   = cache('cms_moudle');
    }

    /**
     * 栏目首页
     */
    public function index()
    {
        $this->assign('category', $this->cmsCategoryCache);
        return $this->fetch();
    }

    /**
     * 栏目的添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->request->has('is_batch')) { //批量模式
                $data['cat_name'] = $this->request->param('cat_names');
            }
            $result = $this->validate($data, 'CmsCategoryValidate');
            if ($result !== true) {
                $this->error($result);
            }
            if ($this->request->has('is_batch')) {
                $catNames = explode("\r\n", $data['cat_name']);
                $dataList = [];
                foreach ($catNames as $v) {
                    unset($data['cat_names']);
                    unset($data['is_batch']);
                    $data['cat_name'] = $v;
                    $dataList[]       = $data;
                }
                $this->cmsCategoryModel->saveAll($dataList);
            } else {
                $this->cmsCategoryModel->addData($data);
            }
            $this->cmsCategoryModel->updateCache();
            $this->success('添加成功', url('index'));
        }
        $this->assign('cmsMoudleCache', $this->cmsMoudleCache);
        $this->assign('category', $this->cmsCategoryCache);
        return $this->fetch();
    }

    /**
     * 修改栏目
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'CmsCategoryValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $this->cmsCategoryModel->editData($data);
            $this->cmsCategoryModel->updateCache();
            $this->success('操作成功！', url('index'));
        }
        $cid      = $this->request->param('cid/d', 0);
        $info     = $this->cmsCategoryCache[$cid];
        $category = [];
        foreach ($this->cmsCategoryCache as $k => $v) {
            //父栏目select状态
            $v['selected'] = $info['pid'] == $v['cid'] ? 'selected' : '';
            //子栏目disabled
            $disabled      = \org\util\TreeUtil::isChild($this->cmsCategoryCache, $v['cid'], $cid, 'cid') || $cid == $v['cid'] ? 'disabled' : '';
            $v['disabled'] = $disabled;
            $category[]    = $v;
        }
        $dataTpl = str_replace_limit('cms_', '', $this->cmsMoudleCache[$info['mid']]['table_name']);
        $this->assign('dataTpl', $dataTpl);
        $this->assign('category', $category);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除栏目
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $mid = $this->request->param('mid/d');
            $cid = $this->request->param('cid/d');
            if ($this->cmsCategoryModel->where('pid', $cid)->find()) {
                $this->error('该栏目还存在下级分类，不允许删除！');
            } else {
                if (cms_moudle($mid)->where('cid', $cid)->find()) {
                    $this->error('该栏目下还存在数据，不允许删除！');
                }
                $this->cmsCategoryModel->delData($cid);
                $this->cmsCategoryModel->updateCache();
                $this->success('删除成功！');
            }
        }
    }

    /**
     * 更新站点缓存
     */
    public function updateCache()
    {
        $this->cmsCategoryModel->updateCache();
        $this->success('操作成功！', url('index'));
    }

    /**
     *栏目排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $cid => $order) {
            $cid        = intval($cid);
            $order      = intval($order);
            $dataList[] = ["cid" => $cid, "cat_order" => $order];
        }
        $this->cmsCategoryModel->saveAll($dataList);
        $this->updateCache();
        $this->success('排序成功！');
    }
    /**
     *栏目模板批量设置
     */
    public function updateTpl()
    {
        $listTpl = $this->request->param('list_tpl/a');
        foreach ($listTpl as $cid => $cat_tpl) {
            $cid        = intval($cid);
            $cat_tpl      = $cat_tpl;
            $dataList[] = ["cid" => $cid, "cat_tpl" => $cat_tpl];
        }
        $this->cmsCategoryModel->saveAll($dataList);
        $this->updateCache();
        $this->success('栏目模板设置成功！');
    }
}
